//
//  SingleSelBtnView.swift
//  SingleSelectBtnDemo
//
//  Created by healthmanage on 16/9/28.
//  Copyright © 2016年 healthmanager. All rights reserved.
//

import Foundation
import UIKit

class SingleSelBtnView: UIView {
    
    var clickBlock:((Int) -> ())?
    
    var currentTag = Int();
    
    func hSingleSelBtn(_ titleArray:NSArray, typeE aTypeE:Int, defaultV: Int) {
        
        let frameE:CGRect = self.frame;
        let aWidthH:CGFloat = frameE.width;
        let aHeightT:CGFloat = frameE.height;
        
        if aTypeE==1 {
            //横向
            let widthH:CGFloat = (aWidthH-20-30*CGFloat(titleArray.count))/CGFloat(titleArray.count);
            
            for i:Int in 0 ..< titleArray.count {
                
                let btn = UIButton.init(frame: CGRect(x: 10+(widthH+20)*CGFloat(i), y: (aHeightT-16)/2, width: 16, height: 16));
                btn.setImage(UIImage.init(named: "unchoose"), for: UIControlState());
                btn.setImage(UIImage.init(named: "choose"), for: .selected);
                if i==defaultV {
                    btn.isSelected = true;
                }else{
                    btn.isSelected = false;
                }
                btn.tag = i+10000;
                btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside);
                self.addSubview(btn);
                
                let labelL = UILabel.init(frame: CGRect(x: 40+(widthH+20)*CGFloat(i), y: 0, width: widthH, height: aHeightT));
                labelL.text = titleArray[i] as? String;
                labelL.adjustsFontSizeToFitWidth = true;
                self.addSubview(labelL);
            }
        }else{
            //纵向
            let hidthH = CGFloat(aHeightT / CGFloat(titleArray.count));
            
            for i:Int in 0 ..< titleArray.count {
                
                let btn = UIButton.init(frame: CGRect(x: 10, y: (hidthH-16)/2 + hidthH * CGFloat(i), width: 16, height: 16));
                btn.setImage(UIImage.init(named: "unchoose"), for: UIControlState());
                btn.setImage(UIImage.init(named: "choose"), for: .selected);
                if i==defaultV {
                    btn.isSelected = true;
                }else{
                    btn.isSelected = false;
                }
                btn.tag = i+10000;
                btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside);
                self.addSubview(btn);
                
                let labelL = UILabel.init(frame: CGRect(x: 40, y: hidthH * CGFloat(i), width: aWidthH, height: hidthH));
                labelL.text = titleArray[i] as? String;
                labelL.adjustsFontSizeToFitWidth = true;
                self.addSubview(labelL);
            }
        }
        currentTag = 10000;
    }
    
    func btnClick(_ btn:UIButton) {
        if !btn.isSelected {
            btn.isSelected = !btn.isSelected;
            //上一个按钮还原
            let buttonN = self.viewWithTag(currentTag) as? UIButton;
            buttonN?.isSelected = false;
            
            currentTag = btn.tag;
            
            clickBlock!(btn.tag)
        }
    }
}

